package com.nodecollege.cloud.controller;

import com.nodecollege.cloud.common.annotation.ApiAnnotation;
import com.nodecollege.cloud.common.annotation.UserLoginAnnotation;
import com.nodecollege.cloud.common.constants.NCConstants;
import com.nodecollege.cloud.common.model.NCResult;
import com.nodecollege.cloud.common.model.QueryVO;
import com.nodecollege.cloud.common.model.po.UpmsUser;
import com.nodecollege.cloud.common.model.po.UpmsUserFriend;
import com.nodecollege.cloud.common.utils.ThreadUserUtils;
import com.nodecollege.cloud.service.UserFriendService;
import com.nodecollege.cloud.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author LC
 * @date 2020/2/23 16:50
 */
@RestController
@UserLoginAnnotation
@RequestMapping("/myFriend")
public class MyFriendController {

    @Autowired
    private UserFriendService userFriendService;

    @Autowired
    private UserService userService;

    @ApiAnnotation(modularName = "我的好友", description = "获取我的朋友列表！")
    @PostMapping("/getMyFriendList")
    public NCResult<UpmsUserFriend> getMyFriendList(@RequestBody UpmsUserFriend userFriend) {
        userFriend.setUserId(ThreadUserUtils.getLoginUser().getUserId());
        userFriend.setState(NCConstants.INT_2);
        return NCResult.ok(userFriendService.getFriendList(new QueryVO<>(userFriend)));
    }

    @ApiAnnotation(modularName = "我的好友", description = "获取加我好友的请求列表！")
    @PostMapping("/getNewFriendList")
    public NCResult<UpmsUserFriend> getNewFriendList(@RequestBody UpmsUserFriend userFriend) {
        userFriend.setUserId(ThreadUserUtils.getLoginUser().getUserId());
        userFriend.setState(NCConstants.INT_0);
        return NCResult.ok(userFriendService.getFriendList(new QueryVO<>(userFriend)));
    }

    @ApiAnnotation(modularName = "我的好友", description = "获取被我拒绝的请求列表！")
    @PostMapping("/getNotAgreeFriendList")
    public NCResult<UpmsUserFriend> getNotAgreeFriendList(@RequestBody UpmsUserFriend userFriend) {
        userFriend.setUserId(ThreadUserUtils.getLoginUser().getUserId());
        userFriend.setState(NCConstants.INT_3);
        return NCResult.ok(userFriendService.getFriendList(new QueryVO<>(userFriend)));
    }

    @ApiAnnotation(modularName = "我的好友", description = "获取被我加入黑名单的好友列表！")
    @PostMapping("/getBlackList")
    public NCResult<UpmsUserFriend> getBlackList(@RequestBody UpmsUserFriend userFriend) {
        userFriend.setUserId(ThreadUserUtils.getLoginUser().getUserId());
        userFriend.setState(NCConstants.INT_NEGATIVE_1);
        return NCResult.ok(userFriendService.getFriendList(new QueryVO<>(userFriend)));
    }

    @ApiAnnotation(modularName = "我的好友", description = "根据昵称查询用户信息！")
    @PostMapping("/getUserListByNickname")
    public NCResult<UpmsUser> getUserListByNickname(@RequestBody QueryVO<UpmsUser> queryVO) {
        return userService.getUserListByNickname(queryVO);
    }

    @ApiAnnotation(modularName = "我的好友", description = "添加好友！")
    @PostMapping("/addFriend")
    public NCResult addFriend(@RequestBody UpmsUserFriend userFriend) {
        userFriend.setUserId(ThreadUserUtils.getLoginUser().getUserId());
        userFriendService.addFriend(userFriend);
        return NCResult.ok();
    }

    @ApiAnnotation(modularName = "我的好友", description = "处理好友请求！")
    @PostMapping("/handleRequest")
    public NCResult handleRequest(@RequestBody UpmsUserFriend userFriend) {
        userFriend.setUserId(ThreadUserUtils.getLoginUser().getUserId());
        userFriendService.handleRequest(userFriend);
        return NCResult.ok();
    }

    @ApiAnnotation(modularName = "我的好友", description = "修改好友信息！")
    @PostMapping("/updateFriend")
    public NCResult updateFriend(@RequestBody UpmsUserFriend userFriend) {
        userFriend.setUserId(ThreadUserUtils.getLoginUser().getUserId());
        userFriendService.updateFriend(userFriend);
        return NCResult.ok();
    }

    @ApiAnnotation(modularName = "我的好友", description = "删除好友！")
    @PostMapping("/delFriend")
    public NCResult delFriend(@RequestBody UpmsUserFriend userFriend) {
        userFriend.setUserId(ThreadUserUtils.getLoginUser().getUserId());
        userFriendService.delFriend(userFriend);
        return NCResult.ok();
    }

    @ApiAnnotation(modularName = "我的好友", description = "添加到黑名单！")
    @PostMapping("/addBlack")
    public NCResult addBlack(@RequestBody UpmsUserFriend userFriend) {
        userFriend.setUserId(ThreadUserUtils.getLoginUser().getUserId());
        userFriendService.addBlack(userFriend);
        return NCResult.ok();
    }

    @ApiAnnotation(modularName = "我的好友", description = "移出黑名单！")
    @PostMapping("/delBlack")
    public NCResult delBlack(@RequestBody UpmsUserFriend userFriend) {
        userFriend.setUserId(ThreadUserUtils.getLoginUser().getUserId());
        userFriendService.delBlack(userFriend);
        return NCResult.ok();
    }

    @ApiAnnotation(modularName = "我的好友", description = "获取朋友列表-内部调用！", accessSource = 2)
    @PostMapping("/getFriendList")
    public NCResult<UpmsUserFriend> getFriendList(@RequestBody QueryVO<UpmsUserFriend> queryVO) {
        queryVO.getData().setState(NCConstants.INT_2);
        return NCResult.ok(userFriendService.getFriendList(queryVO));
    }

}
